/*
 * @Author: ischen
 * @Date: 2024-06-04 13:39:23
 * @LastEditTime: 2024-06-11 10:49:56
 * @FilePath: RS8025.c
 * @Description: RX8025 Driver
 * ischen.x@outlook.com
 * Copyright (c) 2024 by ischen.x@outlook.com, All Rights Reserved. 
 */

#include "RX8025.h"

static uint8_t dec_to_bcd(uint8_t dec)
{
    return (dec / 10 * 16 + dec % 10);
}

static uint8_t bcd_to_dec(uint8_t bcd)
{
    return (10 * (bcd>>4) + (bcd & 0x0F));
}

/**********************************************************************************************/

/**
 * @description: RX8025 IIC通讯接口，写操作
 * @param {uint8_t} reg_addr 目标寄存器地址
 * @param {uint8_t} *data 数据存储地址
 * @param {uint8_t} length 读取数据长度
 * @return {int}
 */
static int rx8025_i2c_mem_write(uint8_t reg_addr, uint8_t *data, uint8_t length)
{
    return 0;
}

/**
 * @description: RX8025 IIC通讯接口，读操作
 * @param {uint8_t} reg_addr 目标寄存器地址
 * @param {uint8_t} *data 数据存储地址
 * @param {uint8_t} length 读取数据长度
 * @return {int}
 */
static int rx8025_i2c_mem_read(uint8_t reg_addr, uint8_t *data, uint8_t length)
{
    return 0;
}

/**********************************************************************************************/

/**
 * @description: 配置RX8025时间
 * @param {struct tm} *time_struct 时间数据
 * @return {int}
 */
int rx8025_set_time(struct tm *time_struct)
{
    rx8025_reg_type reg = {
        .Seconds =  dec_to_bcd(time_struct->tm_sec ),
        .Minutes =  dec_to_bcd(time_struct->tm_min ),
        .Hours =    dec_to_bcd(time_struct->tm_hour),
        .Weekdays = dec_to_bcd(time_struct->tm_wday),
        .Days =     dec_to_bcd(time_struct->tm_mday ),
        .Mounths =  dec_to_bcd(time_struct->tm_mon + 1),
        .years =    dec_to_bcd(time_struct->tm_year - 100),
    };
    return rx8025_i2c_mem_write(0x00, (uint8_t *)&reg, 7);
}

/**
 * @description: 获取RX8025时间
 * @param {struct tm} *time_struct 时间结构体
 * @return {int}
 */
int rx8025_get_time(struct tm *time_struct)
{
    int ret = 0;
    rx8025_reg_type reg = {0};
    ret = rx8025_i2c_mem_read(0x00, (uint8_t *)&reg, 7);
    if (ret == 0){
        time_struct->tm_sec  = bcd_to_dec(reg.Seconds);
        time_struct->tm_min  = bcd_to_dec(reg.Minutes);
        time_struct->tm_hour = bcd_to_dec(reg.Hours);
        time_struct->tm_wday = bcd_to_dec(reg.Weekdays);
        time_struct->tm_mon  = bcd_to_dec(reg.Mounths&0x7F) - 1;
        time_struct->tm_mday = bcd_to_dec(reg.Days);
        time_struct->tm_year = bcd_to_dec(reg.years) + 100; 
        return 0;
    }else{
        time_struct = NULL;
        return 1;
    }
}

#if RX8025_DEBUG
/**
 * @description: 获取RX8025寄存器数据
 * @param {RX8025_REG_Type} *reg 存入地址
 * @return {int}
 */
int rx8025_get_reg(uint8_t *pdata)
{
    return rx8025_i2c_mem_read(0x00, pdata, sizeof(rx8025_reg_type));
}

/**
 * @description: 设置RX8025寄存器数据
 * @param {RX8025_REG_Type} *reg 存入地址
 * @return {int}
 */
int rx8025_set_reg(uint8_t *pdata)
{
    return rx8025_i2c_mem_write(0x00, pdata, sizeof(rx8025_reg_type));
}
#endif
